<?php
namespace app\blog\controller;

use app\blog\controller\Front;

class Detail extends Front
{
    protected function initialize()
    {
        parent::initialize(); 
    }
    
    public function index()
    {
        //参数判断
        if( isset($this->query['id']) ){
            $data = model('blog/Index','loglic')->getId($this->query['id'], config('cache.expire_detail'), 'normal');
        }elseif( isset($this->query['slug']) ){
            $data = model('blog/Index','loglic')->getSlug($this->query['slug'], config('cache.expire_detail'), 'normal');
        }else{
            $data = [];
        }
        //数据存在否
        if(!$data){
            abort(404);
        }
        //私密笔记
        if($data['detail_extend']['password']){
            //管理员直接显示
            if(in_array('admin',$this->user['user_roles'])){
                return $this->dataCommon($data);
            }
            //普通用户验证密码
            return $this->dataPassword($data);
        }
        //通用文章
        return $this->dataCommon($data);
    }

    //私密文章
    private function dataPassword($data=[])
    {
        //验证密码与验证码
        if($this->query['password'] && $this->query['captcha']){
            if(!captcha_check($this->query['captcha'])){
                $this->error('验证码错误');
            };
            if($this->query['password'] != $data['detail_extend']['password']){
                $this->error('密码错误');
            }
            return $this->dataCommon($data);
        }
        //加载密码表单
        $this->site['title'] = $this->seoReplace($data['detail_name'],true);
        $this->site['keywords'] = $this->seoReplace(implode(',',xfArrayTrim(array_merge($data['tag_name'],explode(',',$data['detail_keywords'])))));
        $this->site['description'] = $this->seoReplace(xfEmpty($data['detail_description'],$data['detail_excerpt']));
        $this->assign($data);
        $this->assign($this->site);
        return $this->fetch('password');
    }

    //数据处理
    private function dataCommon($data=[])
    {
        //主分类
        $type = current($data['type']);
        //写入人气
        model('common/Detail','loglic')->incUpdate($data['detail_id'],'detail_view',1);
        //SEO标签
        $this->site['title'] = $this->seoReplace($data['detail_name'],true);
        $this->site['keywords'] = $this->seoReplace(implode(',',xfArrayTrim(array_merge($data['tag_name'],explode(',',$data['detail_keywords'])))));
        $this->site['description'] = $this->seoReplace(xfEmpty($data['detail_description'],$data['detail_excerpt']));
        //分享链接
        $this->site['shareUrl'] = $this->request->root(true).xfUrlDetail($data);
        //模板赋值
        $this->assign($data);
        $this->assign($type);
        $this->assign($this->site);
        //加载自定义模板
        if($data['detail_tpl']){
            return $this->fetch($data['detail_tpl']);
        }
        //继承主分类模板
        if($type['type_tpl_detail']){
            return $this->fetch($type['type_tpl_detail']);
        }
        //默认加载
        return $this->fetch();
    }
}